package com.jww.hwork04;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/*
编写servletConfig的案例
1. 配置servlet参数
2. 打印参数
3. 获取servletContext对象
 */
public class Demo06_config extends HttpServlet {
    private ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.servletConfig = config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(servletConfig);
        //打印servletName
        String servletName = servletConfig.getServletName();
        System.out.println(servletName);

        String encoding = servletConfig.getInitParameter("Encoding");
        System.out.println("encoding = " + encoding);

        String desc = servletConfig.getInitParameter("desc");
        System.out.println("desc = " + desc);

        //获取所有初始化参数名称的枚举？
        Enumeration<String> Names = servletConfig.getInitParameterNames();
        while (Names.hasMoreElements()) {
            String name = Names.nextElement();
            String value = servletConfig.getInitParameter(name);
            System.out.println(name + "," + value);
        }
        ServletContext servletContext = servletConfig.getServletContext();
        System.out.println("servletContext = " + servletContext);

        Object username = servletContext.getAttribute("username");
        System.out.println("username = " + username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
